<?php namespace Common\Model;
use Hdphp\Model\Model;
class Cate extends Model{
	protected $table="category";
	protected $validate=array(
		array('cname','required','分类名不能为空',3,3),
		array('sort','required','分类排序不能为空',3,3)
	); 
	public function store(){
		if(!$this->create()) return false;
		$this->add();
		return true;
	}
	// 获取除了自己和自己的子集以外的分类
	public function getNoMy($cid){
		$cids=$this->getSon($this->get(),$cid);
		$cids[]=$cid;
		// p($cids);die;
		//条件换为字符串\
		// p($cids);
		// exit;
		$where="cid NOT IN (".implode(',', $cids).")";
		// p($where);die;
		$data=$this->where($where)->get();
		// p($data);
		
		return $data;

	}
	public function getMy($cid){
		$cids=$this->getSon($this->get(),$cid);
		$cids[]=$cid;
		//条件换为字符串\
		// p($cids);
		// exit;
		$where="cid IN (".implode(',', $cids).")";
		$CateData=$this->where($where)->get();
		// p($CateData);
		
		return $CateData;
	}
	public function getSon($data,$cid){
		$temp=array();
		foreach($data as $v){
			if($v['pid']==$cid){
				$temp[]=$v['cid'];
				$temp=array_merge($temp,$this->getSon($data,$v['cid']));
			}
		}
		return $temp;
	}
	public function edit(){
		if(!$this->create()) return false;
		$this->save();
		return true;
	}
}